package com.netease.financial.base.lockpattern.patternunlock;

import com.netease.financial.common.util.NfsLog;
import com.netease.financial.domain.interactor.DefaultSubscriber;
import com.netease.financial.domain.interactor.UseCase;
import com.netease.financial.domain.model.AccessTokenCheck;
import com.netease.financial.internal.di.ActivityScope;
import com.netease.financial.mapper.NfsViewModelDataMapper;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by hzwuyufeng on 1/25/16.
 */
@ActivityScope
public class PatternUnlockPresenterImpl implements PatternUnlockPresenter {

    private static final String TAG = PatternUnlockPresenterImpl.class.getSimpleName();

    private PatternUnlockView patternUnlockView;

    private final UseCase checkAccessTokenUseCase;
    private final NfsViewModelDataMapper nfsViewModelDataMapper;

    @Inject
    public PatternUnlockPresenterImpl(
            @Named("check_access_token") UseCase checkAccessTokenUseCase,
            NfsViewModelDataMapper nfsViewModelDataMapper) {
        this.checkAccessTokenUseCase = checkAccessTokenUseCase;
        this.nfsViewModelDataMapper = nfsViewModelDataMapper;
    }

    @Override
    public void setView(PatternUnlockView view) {
        this.patternUnlockView = view;
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        checkAccessTokenUseCase.unsubscribe();
    }

    @Override
    public void initialize() {

    }

    @Override
    public void checkAccessToken() {
        checkAccessTokenUseCase.execute(new AccessTokenCheckSubscriber());
    }

    private void onAccessTokenCheckFinish(AccessTokenCheck model) {
        NfsLog.d(TAG, model.toString());
        NfsLog.d(TAG, "onAccessTokenCheckFinish.isLogin:" + model.isLogin());

        if (model.isLogin()) {
        } else {
            patternUnlockView.showAccessTokenInvalidView();
        }
    }

    private final class AccessTokenCheckSubscriber extends DefaultSubscriber<AccessTokenCheck> {
        @Override
        public void onCompleted() {
            super.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            NfsLog.d(TAG, e.getMessage());
        }

        @Override
        public void onNext(AccessTokenCheck accessTokenCheck) {
            super.onNext(accessTokenCheck);
            PatternUnlockPresenterImpl.this.onAccessTokenCheckFinish(accessTokenCheck);
        }
    }

}
